package com.jsoft.controller;
import com.jsoft.exception.BusinessException;
import com.jsoft.exception.SystemException;
import com.jsoft.util.Result;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.HashMap;
import java.util.Map;

/**
 * 全局异常处理
 */
@RestControllerAdvice
public class ProjectExceptionAdvice {
    @ExceptionHandler(SystemException.class)
    /**
     * 处理系统异常
     */
    public Result dosystemexception(SystemException exception){
    return new Result(exception.getCode(),exception.getMessage(),null);

    }

    /**
     * 处理业务异常
     * @param e
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    public Result dobusinessexception(BusinessException e){

        return new Result(e.getCode(),e.getMessage(),null);
    }

    /**
     * 参数校验异常
     * @param ex
     * @return
     */

//    @ResponseStatus(HttpStatus.BAD_REQUEST)
//    @ExceptionHandler(MethodArgumentNotValidException.class)
//    public Result handleValidationExceptions(MethodArgumentNotValidException ex) {
//        return new Result(Code.PARAMETER_ERR,"参数校验错误",null);
//    }



    /**
     * 其他异常
     * @param ex
     * @return
     */
   @ExceptionHandler(Exception.class)
    public Result doException(Exception ex){
       ex.printStackTrace();
       return new Result(Code.SYSTEM_UNKNOW_ERR,"系统繁忙,请稍后再试",null);
   }

    }

